<?php

/**
 *
 * @author Administrator
 */
class PageImageController {
    // TODO - Insert your code here
    private $path;
    private $lan;

    /**
     */
    function __construct() {

        $this->path = "../src/module/admin/page_image/temp/";
        
        if (isset( $_SESSION[SESSION_LANGUAGE])) {
            $this->lan = $_SESSION[SESSION_LANGUAGE];
        } else {
            $this->lan = DEFAULT_LANGAUGE;
        }
    }

    /**
     * View table page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewFileManager() {

        $template = new XTemplate( $this->path . $this->lan . "/filemanager.html");
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * View gird page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewGrid() {

        $template = new XTemplate( $this->path . $this->lan . "/grid.html");
		
		// assign controller and action
		$template->assign( CONTROLLER, PVALUE_CONTROLLER_PAGE_PAGEIMAGES);
		$template->assign( ACTION, PVALUE_ACTION_SUBMIT_DELETE_MANY);
		$template->assign( TYPE, $_REQUEST[PARAM_TYPE]);
		
		// assign gird title
		$title = $GLOBALS['pageImageTypeArray'][$this->lan][$_REQUEST[PARAM_TYPE]];
		$template->assign( GRIDTITLE, $title);
		
		$db = new DBAccess();
		$list = $db->selectManyByCause( $GLOBALS['pageimageShortColArray'], PAGEIMAGES, array(PAGEIMAGE_TYPE => $_REQUEST[PARAM_TYPE], SQL_AND, PAGEIMAGE_LANGUAGE => $this->lan), array(PAGEIMAGE_POSITION => ASC));
		
		// assign list
		foreach ( $list as $item ) {
			$template->assign( ITEM, $item);
			$template->parse( PAGECONTENT . "." . ITEM);
		}
		
		// assign item count and page count
		$template->assign( TOTALCOUNT, count( $list));
		$template->assign( TOTALPAGE, 1);
		
		// return page
		$template->parse( PAGECONTENT);
		return $template->out( PAGECONTENT);
	}

	/**
	 * View sort page
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewSort() {

		$template = new XTemplate( $this->path . $this->lan . "/sort.html");
		
		// assign controller and action
		$template->assign( CONTROLLER, PVALUE_CONTROLLER_PAGE_PAGEIMAGES);
		$template->assign( ACTION, PVALUE_ACTION_SUBMIT_SORT);
		$template->assign( TYPE, $_REQUEST[PARAM_TYPE]);
		
		// assign gird title
		$title = $GLOBALS['pageImageTypeArray'][$this->lan][$_REQUEST[PARAM_TYPE]];
		$template->assign( SORTTITLE, $title);
		
		$db = new DBAccess();
		$list = $db->selectManyByCause( $GLOBALS['pageimageShortColArray'], PAGEIMAGES, array(PAGEIMAGE_TYPE => $_REQUEST[PARAM_TYPE], SQL_AND, PAGEIMAGE_LANGUAGE => $this->lan), array(PAGEIMAGE_POSITION => ASC));
		
		// assign list
		foreach ( $list as $item ) {
			$template->assign( ITEM, $item);
			$template->parse( PAGECONTENT . "." . ITEM);
		}
		
		// assign item count and page count
		$template->assign( TOTALCOUNT, count( $list));
		$template->assign( TOTALPAGE, 1);
		
		// return page
		$template->parse( PAGECONTENT);
		return $template->out( PAGECONTENT);
	}

	/**
	 * View form page
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewForm() {

		$template = new XTemplate( $this->path . $this->lan . "/form.html");
		
		// assign controller and action
		$template->assign( CONTROLLER, PVALUE_CONTROLLER_PAGE_PAGEIMAGES);
		$template->assign( ACTION, PVALUE_ACTION_SUBMIT_FORM);
		
		// get id
		if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
			$db = new DBAccess();
			$item = $db->selectOneByCause( $GLOBALS['pageimageFullColArray'], PAGEIMAGES, array(PAGEIMAGE_ID => $_REQUEST[PARAM_ID]));
		} else {
			$item = array();
			$item[PAGEIMAGE_TYPE] = $_REQUEST[PARAM_TYPE];
		}
		
		// assign gird title
		$title = $GLOBALS['pageImageTypeArray'][$this->lan][$item[PAGEIMAGE_TYPE]];
		$template->assign( FORMTITLE, $title);
		
		// assign form item
		$template->assign( FORMITEM, $item);
		$template->parse( PAGECONTENT . "." . FORMITEM);
		
		// return page
		$template->parse( PAGECONTENT);
		return $template->out( PAGECONTENT);
	}

	/**
	 * submit form function
	 * @return multitype:
	 */
	public function submitForm() {
		
		// reponse array
		$response_array = array();
		
		// get input
		$item = array();
		$item[PAGEIMAGE_ID] = $_REQUEST[PAGEIMAGE_ID];
		
		$item[PAGEIMAGE_TYPE] = $_REQUEST[PAGEIMAGE_TYPE];
		$item[PAGEIMAGE_IMAGELINK] = $_REQUEST[PAGEIMAGE_IMAGELINK];
		$item[PAGEIMAGE_IMAGETITLE] = $_REQUEST[PAGEIMAGE_IMAGETITLE];
		$item[PAGEIMAGE_IMAGEALT] = $_REQUEST[PAGEIMAGE_IMAGEALT];
		$item[PAGEIMAGE_DESCRIPTION] = $_REQUEST[PAGEIMAGE_DESCRIPTION];
		$item[PAGEIMAGE_LINK] = $_REQUEST[PAGEIMAGE_LINK];
		
		$item[PAGEIMAGE_UPLOADTIME] = getCurrentDateTime();
		$item[PAGEIMAGE_LANGUAGE] = $this->lan;
		$item[PAGEIMAGE_EDITBY] = $_SESSION[SESSION_USERNAME];
		
		// update data to database
		$db = new DBAccess();
		
		if ($item[USER_ID] == 0) {
			if ($db->insertData( $item, PAGEIMAGES)) {
				$response_array[STATUS] = STATUS_OK;
				$response_array[MESSAGE] = getMessage( MESSAGE_INSERT_SUCCESSFUL);
				$response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_PAGE_PAGEIMAGES . "&act=" . PVALUE_ACTION_VIEW_GRID . "&type=" . $item[PAGEIMAGE_TYPE];
			} else {
				$response_array[STATUS] = STATUS_FAIL;
				$response_array[MESSAGE] = getMessage( MESSAGE_INSERT_FAIL);
			}
		} else {
			if ($db->updateData( $item, PAGEIMAGES)) {
				$response_array[STATUS] = STATUS_OK;
				$response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_SUCCESSFUL);
				$response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_PAGE_PAGEIMAGES . "&act=" . PVALUE_ACTION_VIEW_GRID . "&type=" . $item[PAGEIMAGE_TYPE];
			} else {
				$response_array[STATUS] = STATUS_FAIL;
				$response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_FAIL);
			}
		}
		
		// return repnose array;
		return $response_array;
	}

	/**
	 * submit form function
	 * @return multitype:
	 */
	public function submitSort() {
		
		// response array
		$response_array = array();
		
		// get input
		$array = $_POST[PARAM_LIST];
		
		$db = new DBAccess();
		$position = 1;
		
		// update position
		foreach ( $array as $key => $value ) {
			$value = ( int ) $value;
			if ($value == 0) {
				$caterory = array(PAGEIMAGE_ID => $key, PAGEIMAGE_POSITION => $position);
				$position ++;
				$db->updateData( $caterory, PAGEIMAGES);
			}
		}
		
		$response_array[STATUS] = STATUS_OK;
		$response_array[MESSAGE] = MESSAGE_UPDATE_SUCCESSFUL;
		
		// return response array
		return $response_array;
	}

	/**
	 * submit form function
	 * @return multitype:
	 */
	public function submitDeleteOne() {
		
		// reponse array
		$response_array = array();
		
		if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
			
			$db = new DBAccess();
			
			if ($db->deleteData( PAGEIMAGES, $_REQUEST[PARAM_ID])) {
				$response_array[STATUS] = STATUS_OK;
				$response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
			} else {
				$response_array[STATUS] = STATUS_FAIL;
				$response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
			}
		} else {
			$response_array[STATUS] = STATUS_FAIL;
			$response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
		}
		
		// return repnose array;
		return $response_array;
	}

	/**
	 * submit form function
	 * @return multitype:
	 */
	public function submitDelteMany() {
		
		// reponse array
		$response_array = array();
		
		if (isset( $_REQUEST[PARAM_SELECTED])) {
			
			$IDArray = $_REQUEST[PARAM_SELECTED];
			
			if ($IDArray != null && count( $IDArray) > 0) {
				$delete = true;
				$db = new DBAccess();
				foreach ( $IDArray as $id ) {
					if ($db->deleteData( PAGEIMAGES, $id)) {
					} else {
						$delete = false;
					}
				}
				
				if ($delete) {
					$response_array[STATUS] = STATUS_OK;
					$response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
				} else {
					$response_array[STATUS] = STATUS_FAIL;
					$response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
				}
			} else {
				$response_array[STATUS] = STATUS_ERROR;
				$response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
			}
		} else {
			$response_array[STATUS] = STATUS_ERROR;
			$response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
		}
		
		// return repnose array;
		return $response_array;
	}

	/**
	 */
	function __destruct() {
		
		// TODO - Insert your code here
	}
}

?>